// server/middleware/logger.ts
import type { H3Event } from 'h3'

export default defineEventHandler(async (event) => {
  const start = Date.now()
  const { req, res } = event.node

  try {
    // 请求前的逻辑（如添加计时器）
    event.context.requestStart = start

    // 不需要手动调用下一个中间件
    // Nuxt 会自动处理

  } finally {
    // 请求后的逻辑（一定会执行）
    const duration = Date.now() - start
    const logData = {
      method: req.method,
      path: req.url,
      status: res.statusCode,
      duration: `${duration}ms`,
      ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress
    }

    // console.log('[HTTP]', JSON.stringify(logData))
  }
})